package com.southminority.ethnic.controller.admin.research;

import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.controller.research.vo.ResearchProgressVO;
import com.southminority.ethnic.pojo.ResearchProgress;
import com.southminority.ethnic.service.research.ResearchProgressAdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "20 - 研究进展管理API", description = "【后台】科研成果-研究进展的增删改查接口")
@RestController
@RequestMapping("/admin/research/progress")
@CrossOrigin
@RequiredArgsConstructor
public class ResearchProgressAdminController {

    private final ResearchProgressAdminService progressAdminService;

    @GetMapping("/page")
    @Operation(summary = "分页和条件查询研究进展")
    public R<PageResult<ResearchProgressVO>> getProgressPage(
            @Parameter(description = "当前页码", example = "1") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页显示数量", example = "10") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "研究阶段") @RequestParam(required = false) String stage,
            @Parameter(description = "研究领域") @RequestParam(required = false) String researchField,
            @Parameter(description = "状态", example = "1", schema = @Schema(type = "integer", allowableValues = {"1","0"})) @RequestParam(required = false) Integer status
    ) {
        return R.ok(progressAdminService.getProgressPageForAdmin(page, size, keyword, stage, researchField, status));
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "获取研究进展详情（用于编辑）")
    public R<ResearchProgressVO> getDetail(@PathVariable Long id) {
        return R.ok(progressAdminService.getProgressDetailForAdmin(id));
    }

    @PostMapping("/create")
    @Operation(summary = "新增研究进展", description = "JSON字段以字符串提交，如achievements/milestones/participants/imageUrls/attachmentUrls")
    @ApiResponses(@ApiResponse(responseCode = "200", description = "创建成功"))
    public R<ResearchProgressVO> create(
            @RequestBody(description = "研究进展JSON对象", required = true, content = @Content(schema = @Schema(implementation = ResearchProgress.class)))
            @org.springframework.web.bind.annotation.RequestBody ResearchProgress progress) {
        log.info("管理员创建研究进展: {}", progress.getTitle());
        return R.ok(progressAdminService.createProgress(progress));
    }

    @PutMapping("/update/{id}")
    @Operation(summary = "更新研究进展")
    public R<ResearchProgressVO> update(
            @PathVariable Long id,
            @RequestBody(description = "研究进展JSON对象", required = true, content = @Content(schema = @Schema(implementation = ResearchProgress.class)))
            @org.springframework.web.bind.annotation.RequestBody ResearchProgress progress) {
        log.info("管理员更新研究进展: id={}", id);
        return R.ok(progressAdminService.updateProgress(id, progress));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "逻辑删除研究进展")
    public R<Boolean> delete(@PathVariable Long id) {
        log.info("管理员删除研究进展: id={}", id);
        return R.ok(progressAdminService.deleteProgress(id));
    }

    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量删除研究进展")
    public R<Boolean> batchDelete(
            @RequestBody(description = "ID数组", required = true, content = @Content(schema = @Schema(type = "array", implementation = Long.class)))
            @org.springframework.web.bind.annotation.RequestBody List<Long> ids) {
        log.info("管理员批量删除研究进展: ids={}", ids);
        return R.ok(progressAdminService.batchDeleteProgress(ids));
    }

    @PutMapping("/status/{id}")
    @Operation(summary = "更新研究进展状态")
    public R<Boolean> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        return R.ok(progressAdminService.updateProgressStatus(id, status));
    }

    @PutMapping("/featured/{id}")
    @Operation(summary = "设置研究进展重点展示")
    public R<Boolean> setFeatured(@PathVariable Long id, @RequestParam Integer isFeatured) {
        return R.ok(progressAdminService.setProgressFeatured(id, isFeatured));
    }

    @PutMapping("/timeline/{id}")
    @Operation(summary = "设置研究进展时间线显示")
    public R<Boolean> setTimeline(@PathVariable Long id, @RequestParam Integer isTimeline) {
        return R.ok(progressAdminService.setProgressTimeline(id, isTimeline));
    }

    /**
     * 获取所有研究进展列表（用于下拉选项）
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有研究进展列表", description = "提供给前端下拉选择用，过滤已删除(status=-1)的数据")
    public R<List<ResearchProgressVO>> getAllProgress() {
        return R.ok(progressAdminService.getAllProgressForAdmin());
    }
} 